const io = require('socket.io')
const Fn = require('./fn.js')
module.exports = (httpSer) => {
  const ser = io(httpSer)
  proxy.io = ser
  ser.on('connect', (client) => {
    client.on('find', async(ns, get = {}, opt = {}, cb = () => {}) => {
      if (Acan.isFun(get)) cb = get, get = {}
      if (Acan.isFun(opt)) cb = opt, opt = {}
      Thm.proxyTable('find', ns, get, opt, cb)
    })
    client.on('findOne', async(ns, get = {}, opt = {}, cb = () => {}) => {
      if (Acan.isFun(get)) cb = get, get = {}
      if (Acan.isFun(opt)) cb = opt, opt = {}
      Thm.proxyTable('findOne', ns, get, opt, cb)
    })
    client.on('count', async(ns, get = {}, opt = {}, cb = () => {}) => {
      if (Acan.isFun(get)) cb = get, get = {}
      if (Acan.isFun(opt)) cb = opt, opt = {}
      Thm.proxyTable('count', ns, get, opt, cb)
    })
    client.on('update', async(ns, con, post = {}, cb = () => {}) => {
      Thm.proxyTable('update', ns, con, post, cb)
    })
    client.on('create', async(ns, post = {}, cb = () => {}) => {
      Thm.proxyTable('create', ns, post, cb)
    })
    client.on('creates', async(ns, post = [], cb = () => {}) => {
      Thm.proxyTable('creates', ns, post, cb)
    })
    client.on('createTable', async(ns, cols = {}, opt = {}, cb) => {
      const _t = Fn._t()
      Thm.proxy('createTable', ns, cols, opt, (rs) => {
        if (cb) cb({ hs: Fn._tf(Fn._t() - _t), ns: rs.ns, cols: rs.cols, opt: rs.opt, inc: rs.inc, hourList: rs.hourList, oldInc: rs.oldInc })
      })
    })
  })
  return ser
}